import * as echarts from "echarts";
import { EChartsOption } from "echarts";
import chinaJson from "../data/china.json";

// 注册地图
echarts.registerMap("china", chinaJson);

export default function useEcharts(el: HTMLElement) {
  const echartsInstance = echarts.init(el);
  const setOption = (option: EChartsOption) => {
    echartsInstance.setOption(option);
  };
  // 修改图表的大小，供外界使用的
  const resizeEchart = () => {
    echartsInstance.resize();
  };
  // 监听窗口大小的变化，修改Echarts图表的大小
  window.addEventListener("resize", () => {
    echartsInstance.resize();
  });

  return {
    echartsInstance,
    setOption,
    resizeEchart
  };
}
